<template>
  <div class="app-wrapper" id="app-wrapper" :class="{ Mobile: isMobile }">
    <div class="main-container">
      <div class="sidebar-wrapper">
        <sidebar
          :isMobile="isMobile"
          class="sidebar-container"
          :detail="false"
          :lightHeight="3"
        ></sidebar>
      </div>
      <div
        class="main-layout"
        id="main-layout"
        :style="{
          left: isMobile ? '0' : '50px',
          padding: isMobile ? '1px' : '20px 40px 20px 20px',
          top: isMobile ? '50px' : '0',
          'overflow-y': isMobile ? 'scroll' : isScroll ? 'scroll' : '',
        }"
      >
        <div class="inspection_container">
          <header class="header clearfix">
            <span class="title"></span>&nbsp; &nbsp;设计进展
            <div class="tab">
              <div
                class="item"
                :class="{ active: !selected }"
                @click="toggleView(0)"
              >
                初步设计
              </div>
              <div
                class="item"
                :class="{ active: selected }"
                @click="toggleView(1)"
              >
                施工图设计
              </div>
            </div>
          </header>
          <div class="sub-header clearfix">
            <div class="left">已交总数: <span>{{total}}</span></div>
            <div class="status">
              <div class="item">
                <div class="color-close close"></div>
                <div class="text">计划完成(累计)</div>
              </div>
              <div class="item">
                <div class="color-unclose close"></div>
                <div class="text">实际完成(累计)</div>
              </div>
            </div>
          </div>
          <div class="charts"
          :style="{ 'flex': isMobile ? '': 1, 'height': isMobile ? '350px': ''}">
            <div id="design-chart"></div>
          </div>
          <div class="footer-list animated mt-10" id="animated">
            <div
              class="table-box"
              id="table-box"
              style="height: 100%"
              :style="{
                height: isMobile ? 'auto' : 270 + 'px',
                padding: isMobile ? '10px 0px' : '10px 0px',
              }"
            >
              <table class="table" :data="tableDataList" style="width: 100%">
                <thead>
                  <th
                    :class="{ 'b-r': index === 1 }"
                    v-for="(item, index) in tableDataList[0]"
                    :key="index"
                  >
                    {{ item }}
                  </th>
                </thead>
                <tbody>
                  <tr
                    v-for="(item, index) in tableDataList"
                    :key="index"
                    v-show="index"
                  >
                    <td
                      :class="{ 'f-16': subIndex === 0, 'b-r': subIndex === 1 }"
                      v-for="(subItem, subIndex) in item"
                      :key="subIndex"
                    >
                      {{ subItem }}
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>

import { dcesignProgressspec, dcesignProgressVolume } from '../../api/api'
import * as echarts from 'echarts'
import { isMobile } from '../../utils/index'
import Sidebar from '../../components/Sidebar.vue'
// import a from './a'
export default {
  name: 'Cashflow',
  components: {
    Sidebar
  },
  data () {
    return {
      total: 0,
      selected: 0,
      myChart: null,
      tableDataList: [],
      isScroll: false,
      itemDataList: [], // 现金流列表
      footerHeight: 300, // 项目现金流表 高度
      isMobile: false,
      config: {
        grid: {
          left: 60,
          top: this.isMobile ? 50 : 30,
          right: 10,
          bottom: this.isMobile ? 40 : 60
        },
        color: ['#9ed286', '#00b3f6'],
        xAxis: {
          type: 'category',
          splitLine: { show: false },
          axisTick: {
            show: false
          },
          axisLine: {
            symbol: ['none', 'arrow'],
            lineStyle: {
              color: '#40546c',
              width: 2
            }
          },
          data: [],
          axisLabel: {
            show: true,
            showMinLabel: true,
            showMaxLabel: true,
            margin: 0,
            color: '#fff',
            fontSize: 16,
            fontWeight: '700',
            formatter: function (value) {
              return `\n ${value}`
            }
          }
        },
        yAxis: {
          type: 'value',
          splitLine: { show: false },
          data: [0, 25, 50, 75, 100],
          axisLine: {
            show: true,
            symbol: ['none', 'arrow'],
            symbolSize: [12, 16],
            symbolOffset: [11, 12],
            lineStyle: {
              color: '#40546c',
              width: 2
            }
          },
          axisTick: {
            show: true,
            length: 6,
            lineStyle: {
              color: '#40546c',
              width: 2
            }
          },
          max: function (value) {
            return value.max + 20
          },
          axisLabel: {
            // 调整左侧Y轴刻度， 直接按对应数据显示
            show: true,
            showMinLabel: true,
            showMaxLabel: true,
            margin: 2,
            color: '#fff',
            fontSize: 16,
            fontWeight: '700',
            formatter: function (value) {
              if (value === 0) {
                return ''
              } else if (value > 100) {
                return ''
              } else {
                return value + `%   `
              }
            }
          }
        },
        series: [ {
          name: '',
          type: 'bar',
          barGap: 0,
          barWidth: 20,
          data: [],
          label: {
            show: true,
            offset: [-10, 0],
            formatter: function (val) {
              console.log(val.data)
              return parseInt(val.data) + '%'
            },
            color: '#fff',
            position: 'top',
            fontSize: 14,
            fontWeight: '700'
          },
          itemStyle: {
            borderColor: '#60655e',
            borderWidth: 1
          }
        },
        {
          name: '',
          type: 'bar',
          barGap: 0,
          barWidth: 20,
          data: [],
          label: {
            show: true,
            offset: [10, 0],
            formatter: function (val) {
              return parseInt(val.data) + '%'
            },
            color: '#fff',
            fontSize: 14,
            fontWeight: '700',
            position: 'top'
          },
          itemStyle: {
            borderColor: '#60655e',
            borderWidth: 1
          }
        }]
      }
    }
  },
  methods: {
    toggleView: function (select) {
      this.selected = select
      if (select) {
        this.dcesignProgressspec()
      } else {
        this.dcesignProgressVolume()
      }
    },

    /**
     * 渲染图标数据
     * */
    renderChart: function () {
      this.isMobile && $('#design-chart').css('width', $(window).width() - 10 + 'px')
      if (this.myChart) {
        this.myChart.dispose()
      }
      if (document.getElementById('design-chart')) {
        this.myChart = echarts.init(document.getElementById('design-chart'))
        this.myChart.setOption(this.config)
      }
    },

    dcesignProgressspec: async function () {
      const projectId = this.$route.query.projectId
      const response = await dcesignProgressspec(projectId)
      this.doWithData(response)
    },

    dcesignProgressVolume: async function () {
      const projectId = this.$route.query.projectId
      const response = await dcesignProgressVolume(projectId)
      this.doWithData(response)
    },
    doWithData: function (list = []) {
      const data = [
        ['数据', '总计'],
        ['总数据', 0],
        ['计划提交数量', 0],
        ['计划完成百分比', 0],
        ['实际提交数量', 0],
        ['实际完成百分比', 0]
      ]
      this.config.xAxis.data = []
      list.forEach((item) => {
        data[0].push(item.name)
        this.config.xAxis.data.push(item.name)
        data[1][1] += Number(item.total)
        data[2][1] += Number(item.planSubQuantity)
        data[4][1] += Number(item.actSubQuantity)
      })
      this.total = data[1][1]
      data[3][1] = Number(data[1][1] === 0 ? 0 : (data[2][1] / data[1][1]) * 100).toFixed(2) + '%'
      data[5][1] = Number(data[1][1] === 0 ? 0 : (data[4][1] / data[1][1]) * 100).toFixed(2) + '%'
      this.config.series[0].data = []
      this.config.series[1].data = []
      list.forEach((item) => {
        data[1].push(Number(item.total))
        data[2].push(Number(item.planSubQuantity))
        data[3].push(parseInt(Number(item.planFinishPercent) * 100) + '%')
        this.config.series[0].data.push(parseInt(Number(item.planFinishPercent) * 100))
        data[4].push(Number(item.actSubQuantity))
        data[5].push(Number(item.actFinishPercent) * 100 + '%')
        this.config.series[1].data.push(parseInt(Number(item.actFinishPercent) * 100))
      })
      this.tableDataList = data
      this.renderChart()
    }
  },
  created () {
    this.isMobile = isMobile()
  },
  mounted: async function () {
    this.footerHeight = this.isMobile
      ? $(window).height() - 200 - 134
      : $(window).height() - 300 - 134
    this.dcesignProgressVolume()
    this.$forceUpdate()
    $(window).resize(() => {
      this.isMobile = isMobile()
      this.footerHeight = this.isMobile
        ? $(window).height() - 200 - 134
        : $(window).height() - 350 - 134
      this.myChart && this.myChart.resize()
      this.$forceUpdate()
    })
    this.renderChart()
  }
}
</script>
<style lang="scss" scoped>
.inspection_container {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding-top: 12px;
}
.header {
  height: 40px;
  width: 100%;
  display: flex;
  align-content: center;
  margin-bottom: 2px;
  line-height: 40px;
  color: #fff;
  font-size: 16px;
  color: #fff;
  font-weight: 700;
  position: relative;
  .title {
    margin-left: 10px;
    display: inline-block;
    margin-top: 12px;
    height: 14px;
    line-height: 14px;
    border-left: 4px solid #008bf3;
    margin-right: 10px;
  }
  .tab {
    position: absolute;
    right: 30px;
    .item {
      cursor: pointer;
      float: left;
      text-align: center;
      width: 90px;
      font-size: 16px;
      font-weight: 700;
      height: 50px;
      margin: 0 5px;
      border-bottom: 2px solid #ccc;
      color: #ccc;
    }
    .active {
      border-bottom: 2px solid #fff;
      color: #fff;
    }
  }
}
.sub-header {
  height: 20px;
  width: 100%;
  position: relative;
  .left {
    position: absolute;
    left: 0;
    width: 200px;
    color: #fff;
    font-size: 16px;
    font-weight: 700;
    span {
      color: #fff;
      font-size: 16px;
      font-weight: 700;
      text-decoration: underline;
    }
  }
  .status {
    position: absolute;
    right: 0;
    top: 20px;
    width: 320px;
    display: flex;
    .item {
      flex: 1;
      display: flex;
      .text {
        color: #fff;
        font-size: 14px;
        font-weight: 700;
      }
      .close {
        margin-right: 8px;
        width: 30px;
        height: 20px;
        border: 1px solid #60655e;
      }
      .color-close {
        background: #9ed286;
      }
      .color-unclose {
        background: #00b3f6;
      }
    }
  }
}

.charts {
  #design-chart {
    width: 100%;
    height: 100%;
  }
}

.footer-list {
  // background: #ffffff;
  overflow: hidden;
  header {
    height: 40px;
    line-height: 40px;
    width: 100%;
    // background: #ffffff;
    margin-bottom: 2px;
    .title {
      margin-left: 30px;
      margin-top: 11px;
      height: 18px;
      line-height: 18px;
      padding-left: 10px;
      border-left: 2px solid #008bf3;
      font-weight: 700;
      font-size: 17px;
    }
  }
}
.table-box {
  padding: 10px 15px;
  box-shadow: -10px 0px 15px #082d3c inset,  0px -10px 15px #082d3c inset, 10px 0px 15px #082d3c inset, 0px 10px 15px #082d3c inset;
    border: 1px solid #082d3c;
  .table {
    th,
    td {
      color: #fff;
      text-align: center;
    }

    th {
      font-size: 16px;
      font-weight: 700;
    }
    td {
      font-size: 14px;
    }
    thead {
      // background: #004f7b;
    }
    tr {
      // background: #22559b;
    }
    .f-16 {
      font-size: 16px;
    }
    .b-r {
      border-right: 1px solid #fff;
    }
  }
}

tr:nth-child(2n) {
    background-color: #061a30;
    box-shadow: -10px 0px 15px #034c6a inset, 10px 0px 15px #034c6a inset;
}

.flag {
  vertical-align: middle;
}

.title-loading {
  height: 20px;
  margin-top: 10px;
  width: 60%;
}
</style>
